package com.zys.utils;

import java.util.Date;
import java.util.Calendar;

import java.text.SimpleDateFormat;

/**
 * 日期操作工具类
 */
public class DateUtils {
  /**
   * 日期转换-  String -> Date
   *
   * @param dateString 字符串时间
   * @return Date类型信息
   * @throws Exception 抛出异常
   */
  public static Date parseString2Date(String dateString) throws Exception {
    return dateString == null ? null : parseString2Date(dateString, "yyyy-MM-dd");
  }

  /**
   * 日期转换-  String -> Date
   *
   * @param dateString 字符串时间
   * @param pattern    格式模板
   * @return Date类型信息
   * @throws Exception 抛出异常
   */
  public static Date parseString2Date(String dateString, String pattern) throws Exception {
    return dateString == null ? null : new SimpleDateFormat(pattern).parse(dateString);
  }

  /**
   * 日期转换 Date -> String
   *
   * @param date Date类型信息
   * @return 字符串时间
   */
  public static String parseDate2String(Date date) {
    return date == null ? null : parseDate2String(date, "yyyy-MM-dd");
  }

  /**
   * 日期转换 Date -> String
   *
   * @param date    Date类型信息
   * @param pattern 格式模板
   * @return 字符串时间
   */
  public static String parseDate2String(Date date, String pattern) {
    return date == null ? null : new SimpleDateFormat(pattern).format(date);
  }

  /**
   * 获取当天的 0 点
   *
   * @return 当天零点的 Date对象
   */
  public static Date getTodayZero() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    return calendar.getTime();
  }

  /**
   * 获取当天的 24 点
   *
   * @return 当天 24 点的 Date对象
   */
  public static Date getToday24() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    return calendar.getTime();
  }

  /**
   * 获取本周一
   *
   * @return 本周周一的 Date 对象
   */
  public static Date getThisWeekMonday() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.set(Calendar.DAY_OF_WEEK, 2);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    return calendar.getTime();
  }

  /**
   * 获取本周周日
   *
   * @return 本周周日的 Date 对象
   */
  public static Date getThisWeekSunday() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.set(Calendar.DAY_OF_WEEK, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 24);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    return calendar.getTime();
  }

  /**
   * 获得本月一日的日期
   *
   * @return Date 对象
   */
  public static Date getFirstDay4ThisMonth() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    return calendar.getTime();
  }

  /**
   * 获取本月最后一日的日期
   *
   * @return Date 对象
   */
  public static Date getLastDay4ThisMonth() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    return calendar.getTime();
  }
}
